﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.WorkOrder
{
    public partial class OperationModify : Common.PageBase
    {

        T_WORKORDEROPERATION_BLL bll = new T_WORKORDEROPERATION_BLL();
        private T_WORKORDERTYPE_BLL workOrderTypeBll = new T_WORKORDERTYPE_BLL();
        private T_WORKORDERSTATUS_BLL workOrderStatusBll = new T_WORKORDERSTATUS_BLL();
        private T_PERMISSION_BLL permissionBll = new T_PERMISSION_BLL();


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlISPOPPAGEBinding();
                ddlORDERTYPEIDBinding();
                ddlCURRENTSTATUSIDBinding();
                ddlNEXTSTATUSIDBinding();

                ddlISUPDATEINFOBinding();
                ddlIsConfirmBinding();
                ddlIsBackListBinding();

                pageDataInit();
            }
        }

     

        #region 页面初始化数据绑定

        private void ddlIsBackListBinding()
        {
            EnumHelper.FillListControl(ddlIsBackList, typeof(IsBackList));
        }

        private void ddlISPOPPAGEBinding()
        {
            EnumHelper.FillListControl(ddlISPOPPAGE, typeof(IsPopPage));
        }
        /// <summary>
        /// DDLs the is confirm binding.
        /// </summary>
        private void ddlIsConfirmBinding()
        {
            EnumHelper.FillListControl(ddlIsConfirm, typeof(IsConfirm));
        }

        /// <summary>
        /// DDLs the ISUPDATEINFO binding.
        /// </summary>
        private void ddlISUPDATEINFOBinding()
        {
            EnumHelper.FillListControl(ddlISUPDATEINFO, typeof(IsUpdateInfo));
        }
        /// <summary>
        /// DDLs the ORDERTYPEID binding.
        /// </summary>
        private void ddlORDERTYPEIDBinding()
        {
            ddlORDERTYPEID.DataSource = workOrderTypeBll.GetCache_T_WORKORDERTYPE_List(this.Page);
            ddlORDERTYPEID.DataTextField = "OrderTypeName";
            ddlORDERTYPEID.DataValueField = "OrderTypeID";
            ddlORDERTYPEID.DataBind();
        }
        /// <summary>
        ///	当前工单状态编号 下拉列表数据绑定
        /// </summary>
        private void ddlCURRENTSTATUSIDBinding()
        {

            ddlCURRENTSTATUSID.DataSource = workOrderStatusBll.GetSessionList(this.Page, ddlORDERTYPEID.SelectedValue.Trim());
            ddlCURRENTSTATUSID.DataTextField = "OrderStatusName";
            ddlCURRENTSTATUSID.DataValueField = "OrderStatusID";
            ddlCURRENTSTATUSID.DataBind();
            //ddlCURRENTSTATUSID.Items.Insert(0, new ListItem("", ""));
        }
        /// <summary>
        ///	后顺状态编号 下拉列表数据绑定
        /// </summary>
        private void ddlNEXTSTATUSIDBinding()
        {

            ddlNEXTSTATUSID.DataSource = workOrderStatusBll.GetSessionList(this.Page,  ddlORDERTYPEID.SelectedValue.Trim());
            ddlNEXTSTATUSID.DataTextField = "OrderStatusName";
            ddlNEXTSTATUSID.DataValueField = "OrderStatusID";
            ddlNEXTSTATUSID.DataBind();
            //ddlNEXTSTATUSID.Items.Insert(0, new ListItem("", ""));
        }

        #endregion

        private void pageDataInit()
        {
            string _operationid = String.IsNullOrEmpty(Request.QueryString["OPERATIONID"]) ? "" : Request.QueryString["OPERATIONID"].Trim();

            if (!String.IsNullOrEmpty(_operationid))
            {
                T_WORKORDEROPERATION_Entity entity = bll.GetEntity("OPERATIONID", _operationid);
                if(entity != null)
                {
                    T_WORKORDERSTATUS_Entity workOrderStatusEntity =
                        workOrderStatusBll.GetPageItem_T_WORKORDERSTATUS_EntityByKey(this.Page, entity.CURRENTSTATUSID);

                    
                }

                PageHelper.SetControlValue<T_WORKORDEROPERATION_Entity>(this.Page, entity);
                ddlORDERTYPEID_SelectedIndexChanged(null, null);
                ddlNEXTSTATUSID.SelectedValue = entity.NEXTSTATUSID.ToString();
                ddlCURRENTSTATUSID.SelectedValue = entity.CURRENTSTATUSID.ToString();
            }


        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string _operationid = String.IsNullOrEmpty(Request.QueryString["OPERATIONID"]) ? "" : Request.QueryString["OPERATIONID"].Trim();


            T_WORKORDEROPERATION_Entity entity = bll.GetEntity("OPERATIONID", _operationid);


            if (entity != null)
            {
                PageHelper.GetControlValue<T_WORKORDEROPERATION_Entity>(this.Page, ref entity);
                entity.ORDERTYPENAME = ddlORDERTYPEID.SelectedItem.Text;
                entity.CURRENTSTATUSNAME = ddlCURRENTSTATUSID.SelectedItem.Text;
                entity.NEXTSTATUSNAME = ddlNEXTSTATUSID.SelectedItem.Text;

                try
                {
                    long rel = bll.Update(entity);
                    if (rel > 0)
                    {
                        UpdatePermission(entity);
                        PageHelper.ShowMessage(this.Page, "修改成功！", UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this), "Operationlist.aspx"));
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "修改失败！");
                    }
                }
                catch (Exception ex)
                {
                    
                        PageHelper.ShowMessage(this.Page, "修改失败！" + ex.Message);
                }
            }
            else
            {
                entity = new T_WORKORDEROPERATION_Entity();

                PageHelper.GetControlValue<T_WORKORDEROPERATION_Entity>(this.Page, ref entity);
                entity.ORDERTYPENAME = ddlORDERTYPEID.SelectedItem.Text;
                entity.CURRENTSTATUSNAME = ddlCURRENTSTATUSID.SelectedItem.Text;
                entity.NEXTSTATUSNAME = ddlNEXTSTATUSID.SelectedItem.Text;
                try
                {

                    long rel = bll.Insert(entity);
                    if (rel > 0)
                    {
                        UpdatePermission(entity);
                        PageHelper.ShowMessage(this.Page, "添加成功！", "Operationlist.aspx");
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "添加失改！");
                    }
                }
                catch (Exception ex)
                {
                    
                        PageHelper.ShowMessage(this.Page, "添加失改！" + ex.Message);
                }
            }
        }

        /// <summary>
        /// 更新操作表
        /// Updates the permission.
        /// </summary>
        /// <param name="entity">The entity.</param>
        private void UpdatePermission(T_WORKORDEROPERATION_Entity entity)
        {
            T_PERMISSION_Entity permissionEntity;
            WorkOrderType workOrderType =
                (WorkOrderType) Enum.Parse(typeof (WorkOrderType), entity.ORDERTYPEID.ToString());
            PermissionType _permissionType = PermissionType.咨询工单;
            string _pageClassName = "";
            string _permissionCode = "";
            string _permissionName = workOrderType.ToString() + "." + entity.OPERATIONNAME;
            #region MyRegion
            switch (workOrderType)
            {
                case WorkOrderType.咨询工单:
                    {
                        _pageClassName = StaticConstant.ConsultingCasePageClass;
                        _permissionCode = StaticConstant.PreConsultingCase + entity.OPERATIONID.ToString();
                        _permissionType = PermissionType.咨询工单;
                    }
                    break;
                case WorkOrderType.投诉工单:
                    {
                        _pageClassName = StaticConstant.ComplaintCasePageClass;
                        _permissionCode = StaticConstant.PreComplaintCase + entity.OPERATIONID.ToString();
                        _permissionType = PermissionType.投诉工单;
                    }
                    break;
                case WorkOrderType.报修工单:
                    {
                        _pageClassName = StaticConstant.RepairCasePageClass;
                        _permissionCode = StaticConstant.PreRepairCase + entity.OPERATIONID.ToString();
                        _permissionType = PermissionType.报修工单;
                    }
                    break;
                case WorkOrderType.潜在客户:
                    {
                        _pageClassName = StaticConstant.PotentialCustomerCasePageClass;
                        _permissionCode = StaticConstant.PrePotentialCustomerCase + entity.OPERATIONID.ToString();
                        _permissionType = PermissionType.潜在客户;
                    }
                    break;
            } 
            #endregion

            permissionEntity = permissionBll.GetEntity("PermissionCode", _permissionCode);
            if (permissionEntity == null)
            {
                permissionEntity = new T_PERMISSION_Entity();
                permissionEntity.COMANDNAME = _permissionCode;
                permissionEntity.PAGECLASSNAME = _pageClassName;
                permissionEntity.PERMISSIONCODE = _permissionCode;
                permissionEntity.PERMISSIONNAME = _permissionName;
                permissionEntity.TYPE = ((int) _permissionType).ToString();
                permissionEntity.REMARK = entity.CURRENTSTATUSNAME + "->" + entity.NEXTSTATUSNAME;
                permissionEntity.ISCONFIRM = entity.ISCONFIRM;
                permissionBll.Insert(permissionEntity);
            }
            else
            {
                permissionEntity.COMANDNAME = _permissionCode;
                permissionEntity.PAGECLASSNAME = _pageClassName;
                permissionEntity.PERMISSIONNAME = _permissionName;
                permissionEntity.TYPE = ((int)_permissionType).ToString();
                permissionEntity.REMARK = entity.CURRENTSTATUSNAME + "->" + entity.NEXTSTATUSNAME;
                permissionEntity.ISCONFIRM = entity.ISCONFIRM;
                permissionBll.Update(permissionEntity);
            }


            
            
        }

        protected void ddlORDERTYPEID_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlCURRENTSTATUSIDBinding();
            ddlNEXTSTATUSIDBinding();
        }
    }
}
